/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.torresquintero.oncologos.common.vista;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JTextField;

/**
 *
 * @author EAM
 */
public class TextFieldValidador extends JTextField implements KeyListener {

    /**
     * Expresion Regular que valida el contenido del campo.
     */
    public String regExp;

    public TextFieldValidador() {

        super();
        addKeyListener(this);
    }

    public void keyTyped(KeyEvent e) {
        //  throw new UnsupportedOperationException("Not supported yet.");

        String textoActual = getText()+e.getKeyChar();
        System.out.println(textoActual);

        if (regExp != null) {
            boolean acepto = textoActual.matches(regExp);
            System.out.println(acepto);
            if (!acepto) {
                e.setKeyChar((char)e.VK_CLEAR);

            }
        }
    }

    public void keyPressed(KeyEvent e) {
       
    }

    public void keyReleased(KeyEvent e) {

       
    }

    public void setRegExp(String regExp) {
        this.regExp = regExp;
    }
}
